package org.example.controller;

import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@SuppressWarnings("all")
@Slf4j
@RequiredArgsConstructor
@Component
public class RedisInit implements ApplicationRunner {

    @Resource
    private final RedisTemplate<String, Object> redisTemplate;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        for (int i = 0; i < 300; i++) {
            try {
                redisTemplate.opsForValue().set("k" + i, "v" + i);
                log.info("设置成功值: {}", i);
                Object o = redisTemplate.opsForValue().get(redisTemplate.opsForValue().get("k" + i));
                log.info("设置值成功: {}", o);
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                log.error("error: {}", e.getMessage());
            }
        }
        log.info("finished...");
    }
}
